<?php
	include 'header.php';
	include_once("system/main.php");
	if ( isset( $_SESSION['username'] ) ) { ?>
		<div id="vote"><?php
			$pollid = $_GET['id'];
		if (!isset($pollid)) { 
			include_once ("pick_poll.php");
		}
		else {
			$_SESSION['pollid'] = $pollid;
			$user = $_SESSION['username'];
			$userid = $_SESSION['userid'];
			$res1 = $purevote->db_get_desc_name("$pollid", "userid", "pollname", "description");
			$res2 = $purevote->db_get_ifvoted("$pollid", "$userid", "userid");
			foreach ( $res2 as $poll ) {
				$voted = 1;
			}
			if ($_GET['id']!=NULL) {
			?>
			<?php
			}
			if ($voted == 1) {
				echo "Έχεις ψηφίσει σε αυτή την ψηφοφορία.<br />";
				for ($i=1; $i<=10; $i++) {
				$res3 = $purevote->db_get_choice_votes("$pollid", "$i", "choice", "votes");
				foreach ( $res3 as $poll ) {
				$choice[$i] = $poll['choice'];
				$votes[$i] = $poll['votes'];
					}
				}
                $query = '';
                for ( $i = 1; $i <= 10; ++$i ) {
                    if ( $i == 1 ) {
                        $query .= '?';
                    }
                    else {
                        $query .= '&';
                    }
                    $query .= "votes[]=" . $votes[$i];
                }

                ?><img src="vote_pie.php<?php
                echo $query;
                ?>" /><br />
				<center>	
				<div id="voteshow">
					<table>
						<tr id="choice1"><td><?php echo $choice['1']; ?></td><td> : <?php echo $votes['1']; ?> ψήφοι </td></tr>
						<tr id="choice2"><td><?php echo $choice['2']; ?></td><td> : <?php echo $votes['2']; ?>  ψήφοι </td><br />
						<tr id="choice3"><td><?php if ( $choice['3']!=NULL) { echo $choice['3']; ?></td><td> : <?php echo $votes['3']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice4"><td><?php if ( $choice['4']!=NULL) { echo $choice['4']; ?></td><td> : <?php echo $votes['4']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice5"><td><?php if ( $choice['5']!=NULL) { echo $choice['5']; ?></td><td> : <?php echo $votes['5']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice6"><td><?php if ( $choice['6']!=NULL) { echo $choice['6']; ?></td><td> : <?php echo $votes['6']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice7"><td><?php if ( $choice['7']!=NULL) { echo $choice['7']; ?></td><td> : <?php echo $votes['7']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice8"><td><?php if ( $choice['8']!=NULL) { echo $choice['8']; ?></td><td> : <?php echo $votes['8']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice9"><td><?php if ( $choice['9']!=NULL) { echo $choice['9']; ?></td><td> : <?php echo $votes['9']; ?> ψήφοι</td></tr> <?php } ?>
						<tr id="choice10"><td><?php if ( $choice['10']!=NULL) { echo $choice['10']; ?></td><td> : <?php echo $votes['10']; ?> ψήφοι </td><tr> <?php }  ?>
					</table>
					</div>
				</center> 
				<p>Για επιστροφή στη λίστα των φηφοφοριών πατήστε <a href="./browse.php">εδώ</a>.</p><?php	
			}
			else {	?>
				<form action="dovote.php" method="post">
					<?php foreach ( $res1 as $poll ) { ?>
						<span id="nameofpoll"><?php echo $poll['pollname'] ?> </span>
						<div id="descofpoll" "><?php echo $poll['description']; ?> </div>
					<?php }
					$i = 1;
					$temp ='choice'.$i;?><center>
					<table>
						<?php for ($i=1; $i<=10; $i++) {
							$res4 = $purevote->db_get_choice_votes("$pollid", "$i", "choice", "votes");
								foreach ( $res4 as $poll ) {
									if ($poll['choice'] != NULL) {
										echo "<tr><td><input type=\"radio\" name=\"voted\" value=\"$i\" id=\"f$i\"/></td><th><label for=\"f$i\">".$poll['choice']."</label></th></tr>"; 
									}
								}
							}?>
					</table></center><br />	
					<input type="submit" value="Ψήφισε" class="button"/>
				</form>		
				<form action="./vote.php" method="post" class="next">
				<input type="submit" value="Επόμενη" class="button"/>
			</form>
				<p>Για προβολή των φηφοφοριών σε λίστα πατήστε <a href="./browse.php">εδώ</a>.</p>
				<br >

		<?php
			}
			?>
		 </div>
			
	<?php
		}
	}
	else { ?> 
			<p>Πρέπει να είσαι συνδεδεμένος για να μπορείς να ψηφίσεις! </p>
			<p>Αν δεν έχεις λογαριασμό, δημιούργησε έναν  <a href='register.php'>εδώ</a>.</p>
	 <?php }
	include 'footer.php';
?>
